package com.hqyj.controller;

import com.hqyj.pojo.DayInfo;
import com.hqyj.pojo.ManagerInfo;
import com.hqyj.pojo.UserInfo;
import com.hqyj.pojo.UserSerInfo;
import com.hqyj.service.DayInfoService;
import com.hqyj.service.ManagerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

@Controller
@RequestMapping("/manager")
public class ManagerController {
    //创建managerinfoservice的实现类对象
    @Autowired
    ManagerInfoService managerInfoService;

    @Autowired
    DayInfoService dayInfoService;

    //获取图片上传的配置路径
    @Value("${file.address}")
    String  fileAddress;

    //用户访问的图片路径
    @Value("${file.staticPath}")
    String  upload;


    //访问修改密码界面updatePwd
    @RequestMapping("/updatePwd")
    public String updatePwd(){
        return "manager/updatePwd";
    }

    //处理修改密码的ajax请求
    @RequestMapping("/updatePwdAjax")
    @ResponseBody
    public HashMap<String,Object> updatePwdAjax(ManagerInfo manager, HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = managerInfoService.updatePwd(manager,request);
        map.put("info",info);
        return map;
    }

    //访问修改头像页面
    @RequestMapping("/updateHead")
    public String updateHead(){
        return "manager/updateHead";
    }

    //头像上传
    @RequestMapping("/upload")
    @ResponseBody
    public HashMap<String,Object> upload(MultipartFile file){
        HashMap<String,Object> map = new HashMap<String,Object>();

        //上传文件
        try {
            //定义一个文件上传路径
            String timeDir="";
            //获取时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            timeDir=sdf.format(new Date());

            //定义上传文件的前缀
            String pre="";
            //是为了保证文件上传后，存到服务器的文件名是唯一的
            pre= UUID.randomUUID()+"";

            //获取文件的后缀名
            String hou="";
            if(file!=null){
                //.jpg
                String originalName = file.getOriginalFilename();
                hou=originalName.substring(originalName.lastIndexOf(".")+1);
            }

            //定义文件上传的全路径
            String filePath=fileAddress+"\\"+timeDir+"\\"+pre+"."+hou;

            //创建file对象
            File f=new File(filePath);

            //判断目录是否存在，不存在就创建目录
            if (!f.isDirectory()){
                //创建目录
                f.mkdirs();
            }
            //上传文件
            file.transferTo(f);
            //设置成功提示信息
            map.put("code",0);
            //返回给前端 用户访问这个图片的路径
            String path=upload+"\\"+timeDir+"\\"+pre+"."+hou;
            map.put("src",path);
            return map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        //设置上传失败的提示信息
        map.put("code",1);
        return map;
    }

    //头像更改
    @RequestMapping("/saveHead")
    @ResponseBody
    public HashMap<String,Object> saveHead(ManagerInfo manager, HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = managerInfoService.updateHead(manager,request);
        map.put("info",info);
        return map;
    }

    //访问日常安排界面dailyEvent
    @RequestMapping("/dailyEvent")
    public String dailyEvent(DayInfo day, ModelMap m, HttpServletRequest request){
        //取出存入session中的id
        HttpSession session = request.getSession();
        ManagerInfo uu = (ManagerInfo) session.getAttribute("mag");
        day.setDayManager(uu.getManagerNickName());
        System.out.print(day.getDayManager()+"********************************************");
        //查询数据
        HashMap<String, Object> map=dayInfoService.selectUserSer(day);
        //把数据传到前段
        m.put("info",map);
        return "manager/dailyEvent";
    }

    //访问新增日常安排界面daily-add
    @RequestMapping("/dailyAdd")
    public String sendEmail(){
        return "manager/daily-add";
    }

    //处理新增日常安排的请求
    @RequestMapping("/dailyAddAjax")
    @ResponseBody
    public HashMap<String,Object> sendEmail(DayInfo day, HttpServletRequest request){
        //取出存入session中的id获取经理昵称
        HttpSession session = request.getSession();
        ManagerInfo uu = (ManagerInfo) session.getAttribute("mag");
        day.setDayManager(uu.getManagerNickName());

        HashMap<String,Object> map=new HashMap<String, Object>();
        String info=dayInfoService.dailyAdd(day);
        map.put("info",info);
        return map;
    }

    //日常安排标记完成
    @RequestMapping("/tag")
    @ResponseBody
    public HashMap<String,Object> tag(DayInfo day){
        HashMap<String,Object> map=new HashMap<String, Object>();
        String info=dayInfoService.tag(day);
        map.put("info",info);
        return map;
    }

    //处理删除日常安排的ajax请求
    @RequestMapping("/dailyDel")
    @ResponseBody
    public HashMap<String,Object> dailyDel(DayInfo day){
        HashMap<String,Object> map=new HashMap<String, Object>();
        String info=dayInfoService.del(day);
        map.put("info",info);
        return map;
    }

    //访问userbir-list.html界面
    @RequestMapping("/userbir")
    public String userbir(UserInfo user,ModelMap m, HttpServletRequest request){
        //取出存入session中的id
        HttpSession session = request.getSession();
        ManagerInfo mm=(ManagerInfo)session.getAttribute("mag") ;
        //根据managerId查询当前经理
        ManagerInfo manager = managerInfoService.selectByManagerId(mm);
        user.setUserManager(manager.getManagerNickName());
        //获取时间
        SimpleDateFormat sdfMon = new SimpleDateFormat("MM");
        SimpleDateFormat sdfDay = new SimpleDateFormat("dd");
        String monthNow=sdfMon.format(new Date());
        String dayNow=sdfDay.format(new Date());
        user.setMonthNow(monthNow);
        user.setDayNow(dayNow);

        //查询今天生日，且属于当前登录的管理员的客户的个人信息
        HashMap<String, Object> map=managerInfoService.selectUserByBir(user);
        //把数据传到前段
        m.put("info",map);
        return "manager/userbir-list";
    }

    //访问邮件发送界面sendEmail
    @RequestMapping("/sendEmailPage")
    public String sendEmail(UserInfo user, ModelMap m){
        //根据userId查询
        UserInfo u = managerInfoService.selectByUserId(user);
        //把数据传递到前端
        m.addAttribute("user",u);
        return "manager/sendEmailPage";
    }

    //处理邮件发送的请求
    @RequestMapping("/sendEmailAjax")
    @ResponseBody
    public HashMap<String,Object> sendEmail(String email, String emailText){
        return  managerInfoService.sendCode(email,emailText);
    }

    //访问manager-resultqure.html界面
    @RequestMapping("/managerresultPage")
    public String managerresultPage(UserSerInfo ser, ModelMap m, HttpSession session){
        //把当前用户参数传入
        ManagerInfo managerInfo= (ManagerInfo) session.getAttribute("mag");
        //把当前用户名存入pojpo中并备份中也存入
        ser.setSerManager(managerInfo.getManagerNickName());
        //查询分页数据
        HashMap<String, Object> map=managerInfoService.selectresilt(ser);
        //把数据传到前端
        m.put("info",map);
        //把用户输入的查询条件传到前端
        m.put("vv",ser.getConValue());
        return "manager/manager-resultqure";
    }

    //打开查看服务界面
    @RequestMapping("/managereditPage")
    public String managereditPage(UserSerInfo ser, ModelMap m){
        //根据userId查询
        UserSerInfo u =managerInfoService.selectreByserId(ser);
        //把数据传递到前端
        m.addAttribute("ser",u);
        return "manager/manager-edtresultd";
    }

    //处理接受任务信息的ajax请求
    @RequestMapping("/maserveedit")
    @ResponseBody
    public HashMap<String,Object> maserveedit(UserSerInfo ser){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = managerInfoService.maupService(ser);
        map.put("info",info);
        return map;
    }

    //访问user-list页面
    @RequestMapping("/list")
    public String list(UserInfo user, ModelMap m, HttpServletRequest request){
        //获取session
        HttpSession session = request.getSession();
        ManagerInfo u = (ManagerInfo) session.getAttribute("mag");
        user.setUserManager(u.getManagerNickName());
        //System.out.println("-----经理是-------"+user.getUserManager());
        //查询分页数据
        HashMap<String, Object> map=managerInfoService.select(user);
        //把数据传到前段
        m.put("info",map);
        //把用户输入的查询条件传到前端
        m.put("vv",user.getConValue());
        return "manager/user-list";
    }

    //处理表格的ajax数据
    @RequestMapping("/listAjax")
    @ResponseBody
    public HashMap<String,Object> listAjax(UserInfo user){
        //返回分页数据
        return managerInfoService.select(user);
    }

    //打开修改页面
    @RequestMapping("/editPage")
    public String editPage(UserInfo user, ModelMap m){
        //根据userId查询
        UserInfo u = managerInfoService.selectByUserId(user);
        //把数据传递到前端
        m.addAttribute("user",u);
        return "manager/user-edit";
    }

    //处理修改的ajax请求
    @RequestMapping("/edit")
    @ResponseBody
    public HashMap<String,Object> edit(UserInfo user){
        HashMap<String,Object> map=new HashMap<String, Object>();
        String info=managerInfoService.update(user);
        map.put("info",info);
        return map;
    }

    //处理删除的ajax请求
    @RequestMapping("/del")
    @ResponseBody
    public HashMap<String,Object> del(UserInfo user){
        HashMap<String,Object> map=new HashMap<String, Object>();
        String info=managerInfoService.del(user);
        map.put("info",info);
        return map;
    }

    //访问当前用户的数据的页面user-info
    @RequestMapping("/managerPage")
    public String managerPage(ManagerInfo man, ModelMap m, HttpSession session){
        //把当前用户参数传入
        ManagerInfo managerInfo= (ManagerInfo) session.getAttribute("mag");
        //把当前用户名存入pojpo中并备份中也存入
       man.setManagerNickName(managerInfo.getManagerNickName());
        //根据用户名查询
        ManagerInfo u=managerInfoService.selectt(man);
        //把数据传到前端
        m.addAttribute("user",u);
        return "manager/manager-info";
    }

    //处理修改的ajax请求
    @RequestMapping("/maginfo")
    @ResponseBody
    public HashMap<String,Object> usinfo(ManagerInfo mag){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = managerInfoService.magUpdate(mag);
        //System.out.print(info);
        map.put("info",info);
        return map;
    }

}
